﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Besture.Bll;
using Besture.Utils;

namespace Besture.UI.Admin.Sys
{
    public partial class RolesTree : System.Web.UI.Page
    {
        public string _json = string.Empty;

        public List<string> listPermission = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable table = new QueryDac().GetDataTable("SELECT * FROM TBAdminPages tp WHERE tp.ModulesID = 0  ORDER BY tp.SortID ASC");
            

            listPermission = new List<string>();
            string userPermissions = Fun.Query("userPermissions");
            listPermission = StringUtil.SplitStringToList(userPermissions);

            _json = ZtreeJson(table);

        }

        public string ZtreeJson(DataTable table)
        {
            //{id:1, pId:0, name: "父节点1"},
            StringBuilder stringBuilder = new StringBuilder();

            QueryDac dac = new QueryDac();

            stringBuilder.Append("{id:0, pId:-1, name: '所有权限', open:true},");

            foreach (DataRow row in table.Rows)
            {
                string modulesId = row["ID"].ToString();


                stringBuilder.Append("{id:" + row["ID"] + ", pId:0, name: '" + row["Name"] + "', open:true},");
                DataTable pageTable =  dac.GetDataTable("SELECT * FROM TBAdminPages WHERE ModulesID = " + modulesId);

                foreach (DataRow pageRow in pageTable.Rows)
                {
                    string pageId = pageRow["ID"].ToString();
                    string checkEdStr = "";
                    if (listPermission.Contains(pageId))
                    {
                        checkEdStr = ",checked:true";
                    }
                    stringBuilder.Append("{id:" + pageRow["ID"] + ", pId:" + modulesId + ", name: '" + pageRow["Name"] + "', open:true"+checkEdStr+"},");
                }
            }
            string json = string.Format(@"[{0}];", stringBuilder.ToString().TrimEnd(','));
            return json;
        }
    }
}